package com.lidong.lowcode.database.init.core;

import com.lidong.lowcode.database.common.InitDataBaseException;
import com.lidong.lowcode.database.constants.enumconst.SqlTypeEnum;
import com.lidong.lowcode.database.init.intf.CreateTableHandle;
import com.lidong.lowcode.database.init.mysql.MysqlCreateTableHandle;
import com.lidong.lowcode.database.init.mysql.MysqlMetaDataHandle;
import com.lidong.lowcode.database.init.postgresql.PostGreSqlCreateTableHandle;
import com.lidong.lowcode.database.init.postgresql.PostGreSqlMetaDataHandle;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 9/10/2022 11:05 AM
 */
public class DataBaseFactory {

    private static CreateTableHandle createTableHandle;

    /**
     * 获取不同数据库建表实例
     *
     * @param initDataBaseContext 数据库建表上下文
     * @return CreateTableManage
     */
    public static CreateTableHandle getCreateTableManageInstance(InitDataBaseContext initDataBaseContext) {
        SqlTypeEnum sqlType = initDataBaseContext.getSqlType();
        if (null == createTableHandle) {
            if (SqlTypeEnum.MYSQL == sqlType) {
                createTableHandle = new MysqlCreateTableHandle(initDataBaseContext);
            } else if (SqlTypeEnum.POSTGRESQL == sqlType) {
                createTableHandle = new PostGreSqlCreateTableHandle(initDataBaseContext);
            } else {
                throw new InitDataBaseException("SqlType: " + sqlType + " is not supported");
            }
        }
        return createTableHandle;
    }

    /**
     * 获取
     *
     * @param initDataBaseContext 数据库建表上下文
     * @return AbstractCreateTableManage
     */
    public static AbstractCreateTableManage getAbstractCreateTableManage(InitDataBaseContext initDataBaseContext) {
        SqlTypeEnum sqlType = initDataBaseContext.getSqlType();
        if (SqlTypeEnum.MYSQL == sqlType) {
            return new MysqlMetaDataHandle(initDataBaseContext);
        } else if (SqlTypeEnum.POSTGRESQL == sqlType) {
            return new PostGreSqlMetaDataHandle(initDataBaseContext);
        }
        throw new InitDataBaseException("Get abstract createtablemanage error: not a correct type!");
    }
}
